package com.example.myshop.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshop.domain.UserDao;
import com.example.myshop.entity.User;
import com.example.myshop.service.UserService;
import com.example.myshop.service.ex.*;
import com.example.myshop.vo.PriorityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImp extends ServiceImpl<UserDao,User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void reg(User user) {//用户注册
        //通过user参数来获取传递过来的username
        String username=user.getUsername();
        //调用findByUsername(username)判断用户是否被注册过
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User result=userDao.selectOne(queryWrapper);//做一个底层的查询
        //判断结果集是否为null,若不为null则抛出用户名被占用的异常
        if (result !=null){
            //抛出异常
            throw new UsernameDuplicateException("用户名被占用");
        }

        //密码加密处理的实现：md5算法
        //串+password+串 交给md5算法进行加密，连续加载3次
        //串成为盐值  盐值+password+盐值   盐值为一个随机的字符串
        String oldPassword=user.getPassword();
        //获取盐值（随机生成一个盐值）
        String salt= UUID.randomUUID().toString().toUpperCase();//全大写盐值
        user.setSalt(salt);//补全数据库中盐值的记录
        //将密码和盐值作为一个整体进行加密处理
        String md5Password=getMD5Password(oldPassword,salt);
        //将加密之后的密码重新补全设置到user对象中
        user.setPassword(md5Password);

        //补全数据：is_delete设置为0
        user.setIsDelete(0);

        //补全数据：4个日志字段信息
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date =new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);

        //执行注册业务功能的实现(rows==1)
        Integer rows=userDao.insert(user);
        if(rows!=1){
            throw new InsertException("在用户注册过程中产生未知的异常");
        }

    }

    @Override
    public User login(String username, String password) {
        //根据用户名称查询用户数据是否存在，不存在则抛出异常
        //调用mapper层中的findByUsername方法
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User result=userDao.selectOne(queryWrapper);
        if (result==null){
            throw new UserNotFoundException("用户数据不存在");
        }
        //检测用户密码是否匹配
        //1.先获取数据库中加密后的密码
        String oldPassword=result.getPassword();
        //2.与用户传递过来的密码进行比较
        //2.1先获取盐值：上一次在注册中自动生成的盐值
        String salt=result.getSalt();
        //2.2将用户密码按相同md5算法的规则进行加密
        String newMd5Password=getMD5Password(password,salt);
        //将密码进行比较
        if(!newMd5Password.equals(oldPassword)){
            throw new PassWordNotMatchException("用户密码错误");
        }
        //判断is_delete字段的值是否为1表示被标记为删除
        //if(result.getIs_delete()==1){
          //  throw new UserNotFoundException("用户数据不存在");
        //}

        //调用mapper层的findByUsername来查询用户数据,new一个user对象，相当于对要传输的数据进行了压缩提升了系统性能
        User user=new User();
        user.setUid(result.getUid());
        //返回有用户的头像
        user.setAvatar(result.getAvatar());
        user.setUsername(result.getUsername());
        user.setUserType(result.getUserType());
        user.setIsDelete(result.getIsDelete());
        user.setIsPass(result.getIsPass());
        //将当前用户数据返回，返回的数据是为了辅助其他页面做数据展示（user
        return user;
    }

    @Override
    public void changePassword(Long uid, String username, String oldPassword, String newPassword) {
        User result=userDao.selectById(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }

        //原始密码与数据库中密码比较
        String oldMd5Password=getMD5Password(oldPassword,result.getSalt());
        if(!result.getPassword().equals(oldMd5Password)){
            throw new PassWordNotMatchException("密码错误");
        }

        //将新密码设置到数据库中，将新的密码先加密再更新
        String newMd5Password= getMD5Password(newPassword,result.getSalt());
        User user=new User();
        user.setPassword(newMd5Password);
        user.setModifiedTime(new Date());
        user.setUid(uid);
        user.setModifiedUser(username);
        Integer rows= userDao.updateById(user);
        if(rows!=1){
            throw new UpdateException("更新数据时产生未知的异常");
        }
    }


    @Override
    public User getByUid(Long uid) {
        User result = userDao.selectById(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        User user=new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        user.setPhone(result.getPhone());
        user.setEmail(result.getEmail());
        user.setGender(result.getGender());
        user.setLevel(result.getLevel());
        user.setPoint(result.getPoint());

        return user;
    }

    @Override
    public void changeInfo(Long uid, String username, User user) {
        User result = userDao.selectById(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        user.setUid(uid);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows=userDao.updateById(user);
        if(rows!=1){
            throw new UpdateException("更新时数据产生位置异常");
        }
    }

    @Override
    public void changeAvatar(Long uid, String avatar, String username) {
        //查询当前用户数据是否存在
        User result=userDao.selectById(uid);
        if(result==null||result.getIsDelete()==1){
            throw new UserNotFoundException("用户数据不存在");
        }
        User user=new User();
        user.setUid(uid);
        user.setAvatar(avatar);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());
        Integer rows=userDao.updateById(user);
        if(rows!=1){
            throw new UpdateException("更新用户头像时产生未知的异常");
        }
    }

    @Override
    public List<User> checkAllUsers() {
        List<User>users=userDao.selectList(null);
        return users;
    }

    @Override
    public void passUserByUid(Long uid) {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        User result=userDao.selectOne(queryWrapper);
        User user=new User();
        user.setUid(result.getUid());
        user.setIsPass(1);
        user.setModifiedUser("root");
        user.setModifiedTime(new Date());
        userDao.updateById(user);
    }

    @Override
    public void rejectUserByUid(Long uid) {
        User result=userDao.selectById(uid);
        User user=new User();
        user.setUid(result.getUid());
        user.setIsPass(0);
        user.setModifiedUser("root");
        user.setModifiedTime(new Date());
        userDao.updateById(user);
    }

    @Override
    public void addMoney(Long uid, Integer purse, String modifiedUser, Date modifiedTime) {
        User user=new User();
        user.setModifiedTime(modifiedTime);
        user.setModifiedUser(modifiedUser);
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("purse=purse+"+purse);
        userDao.update(user,updateWrapper);
    }

    @Override
    public void subMoney(Long uid, Integer purse, String modifiedUser, Date modifiedTime) {
        User user=new User();
        user.setModifiedTime(modifiedTime);
        user.setModifiedUser(modifiedUser);
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("purse=purse-"+purse);
        userDao.update(user,updateWrapper);
    }

    @Override
    public User getMoney(Long uid) {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        queryWrapper.select("purse","uid");
        User user=userDao.selectOne(queryWrapper);
        return user;
    }

    @Override
    public void addLevel(Long uid, String modifiedUser, Date modifiedTime) {
        UpdateWrapper<User>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("level=level+1");
        User user=new User();
        user.setModifiedUser(modifiedUser);
        user.setModifiedTime(modifiedTime);
        userDao.update(user,updateWrapper);
    }

    @Override
    public void subLevel(Long uid, String modifiedUser, Date modifiedTime) {
        UpdateWrapper<User>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("level=level-1");
        User user=new User();
        user.setModifiedUser(modifiedUser);
        user.setModifiedTime(modifiedTime);
        userDao.update(user,updateWrapper);
    }

    @Override
    public List<User> getLevel() {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_type",2);
        queryWrapper.select("uid","username","user_type","level");
        List<User> users=userDao.selectList(queryWrapper);
        return users;
    }

    @Override
    public User getBidLevel(Long uid) {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_type",2).eq("uid",uid);
        queryWrapper.select("uid","username","user_type","level");
        User user=userDao.selectOne(queryWrapper);
        return user;
    }
    @Override
   public PriorityVO getLevelByBid(Long uid) {
        PriorityVO priorityVO=userDao.getLevelByBid(uid);
        return priorityVO;
    }

    @Override
    public void addPoint(Long uid, Integer point, String modifiedUser, Date modifiedTime) {
        UpdateWrapper<User>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("point=point+" +point);
        User user=new User();
        user.setModifiedTime(modifiedTime);
        user.setModifiedUser(modifiedUser);
        userDao.update(user,updateWrapper);
    }

    @Override
    public void subPoint(Long uid, Integer point, String modifiedUser, Date modifiedTime) {
        UpdateWrapper<User>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("uid",uid).setSql("point=point-" +point);
        User user=new User();
        user.setModifiedTime(modifiedTime);
        user.setModifiedUser(modifiedUser);
        userDao.update(user,updateWrapper);
    }

    @Override
    public User getPoint(Long uid) {
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid).select("uid","username","point");
        User user= userDao.selectOne(queryWrapper);
        return user;
    }
    /**
     * 定义一个md5算法加密处理
     */
    private String getMD5Password(String password,String salt){
        for (int i=0;i<3;i++){
            //md5加密算法方法的调用（进行三次加密，用for循环）
            password= DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();//toUpperCase()转化为全大写
        }
        //返回加密之后的密码
        return  password;
    }


}
